package com.xtianzhuang.www.controller.project;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xtianzhuang.www.annotation.Log;
import com.xtianzhuang.www.annotation.Log.LogType;
import com.xtianzhuang.www.annotation.LogOperate;
import com.xtianzhuang.www.controller.BaseController;
import com.xtianzhuang.www.entity.VInterfaceParam;
import com.xtianzhuang.www.entity.VInterfaceParamExample;
import com.xtianzhuang.www.entity.constants.CodeConstants;
import com.xtianzhuang.www.entity.request.DeleteInterfaceParamsRequest;
import com.xtianzhuang.www.entity.request.QueryInterfaceParamsRequest;
import com.xtianzhuang.www.entity.request.SaveInterfaceParamRequest;
import com.xtianzhuang.www.entity.response.DeleteInterfaceParamsResponse;
import com.xtianzhuang.www.entity.response.QueryInterfaceParamsResponse;
import com.xtianzhuang.www.entity.response.SaveInterfaceParamResponse;
import com.xtianzhuang.www.utils.PageData;

/**
 * 
 * @author xtian.zhuang
 * @date Jul 22, 2019
 */
@Controller
@RequestMapping("/project")
public class InterfaceParamsController extends BaseController {
	@RequestMapping("/ajaxQueryInterfaceParams")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	public QueryInterfaceParamsResponse queryInterfaceParams(QueryInterfaceParamsRequest request) {
		QueryInterfaceParamsResponse response = new QueryInterfaceParamsResponse();
		VInterfaceParamExample example = new VInterfaceParamExample();

		if (StringUtils.isEmpty(request.getKeyword())) {
			example.or().andInterfaceIdEqualTo(request.getInterfaceId());
		} else {
			VInterfaceParamExample.Criteria criteria = example.createCriteria();
			criteria.andInterfaceIdEqualTo(request.getInterfaceId()).andParamCodeLike("%" + request.getKeyword() + "%");
			VInterfaceParamExample.Criteria criteria2 = example.createCriteria();
			criteria2.andInterfaceIdEqualTo(request.getInterfaceId())
					.andParamNameLike("%" + request.getKeyword() + "%");
			example.or(criteria2);
		}
		example.setOrderByClause("param_direction , param_id");
		PageData<VInterfaceParam> data = commonServiceBiz.getPageData(example);
		data.getData().add(new VInterfaceParam());
		response.setCode(CodeConstants.SUCCESS);
		response.setData(data.getData());
		response.setTotal(data.getTotal());
		return response;
	}

	@RequestMapping("/ajaxSaveParam")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	@LogOperate
	@RequiresPermissions(value = {"interface-param-add", "interface-param-update"}, logical = Logical.OR)
	public SaveInterfaceParamResponse saveInterfaceParam(SaveInterfaceParamRequest request) {
		SaveInterfaceParamResponse response = new SaveInterfaceParamResponse();
		try {

			if (request.getParamId() != null) {
				projectServiceBiz.updateInterfaceParam(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("修改成功");
			} else {
				projectServiceBiz.addInterfaceParam(request);
				response.setCode(CodeConstants.SUCCESS);
				response.setMessage("添加成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}

	@RequestMapping("/ajaxDeleteParam")
	@ResponseBody
	@Log(type = LogType.PROJECT)
	@LogOperate
	@RequiresPermissions("interface-param-delete")
	public DeleteInterfaceParamsResponse deleteInterfaceParam(DeleteInterfaceParamsRequest request) {
		DeleteInterfaceParamsResponse response = new DeleteInterfaceParamsResponse();
		try {
			projectServiceBiz.deleteInterfaceParams(request);
			response.setCode(CodeConstants.SUCCESS);
			response.setMessage("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			response.setMessage(e.getMessage());
			response.setCode("9999");
		}
		return response;
	}


}
